package com.itmd.service;

import com.itmd.dao.OrderMapper;
import com.itmd.dao.UserDao;
import com.itmd.dao.imp.UserDapImp;
import com.itmd.domain.Order;
import com.itmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class UserServiceDemo {

    public static void main(String[] args) throws IOException {
//        UserDao userDao =  new UserDapImp();//        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");
////
////        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
//
//        List<User> all = userDao.findAll();

//        System.out.println(all);
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        List<User> userlist = mapper.findAll();
        User user =  mapper.findById(3);


        User condition = new User();
//        condition.setId(1);
        condition.setUsername("moudong");



        //通过sql动态语句查询
        List<User> byUser = mapper.findByUser(condition);

        System.out.println("byuser::"+byUser);


        //通过ids 查询

        List<Integer> ids = new ArrayList<>();
        ids.add(1);
//        ids.add(5);

        List<User> byIds = mapper.findByIds(ids);

        System.out.println("findUsersByIds:"+ byIds);

        System.out.println(user);
        System.out.println(userlist);


        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

        List<Order> all = orderMapper.findAll();
//
//        List<Order> allorderList = orderMapper.findAllorders();
//
//        System.out.println(allorderList);

        System.out.println(all);

    }
}
